package hr.eon.client;

import hr.eon.client.window.LoginWindow;
import hr.eon.core.plugins.EonPluginManager;
import hr.eon.core.plugins.EonPluginManager.ApplicationType;
import org.apache.pivot.beans.BXMLSerializer;
import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.Window;

/**
 * Main class for Client.
 * It start EonPluginManager and EonContext with Spring backend and raise login
 * GUI
 * 
 * @author mjovanovic
 * @since 0.0.1
 */
public class EonClient implements Application {

    private Window login;
    private Display display;

    public static void main( String[] args ) {
        EonPluginManager.init( ApplicationType.CLIENT );
        EonContext.setup();
        DesktopApplicationContext.main( EonClient.class, args );
    }

    @Override
    public void resume() throws Exception {}

    @Override
    public boolean shutdown( boolean arg0 ) throws Exception {
        if ( login != null ) {
            login.close();
        }
        return false;
    }

    @Override
    public void startup( Display display, Map<String, String> arg1 ) throws Exception {
        this.display = display;
        BXMLSerializer bxmlSerializer = new BXMLSerializer();
        bxmlSerializer.getNamespace().put( "application", this );
        EonContext.setSerializer( bxmlSerializer );

        login = (Window) bxmlSerializer.readObject( LoginWindow.class, "xml/LoginWindow.xml" );
        display.getHostWindow().setSize( login.getSize().width, login.getSize().height );
        login.open( display );
    }

    @Override
    public void suspend() throws Exception {}

    public Display getDisplay() {
        return this.display;
    }
}
